package com.lujianfei.productprice.ui.cart;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lujianfei.productprice.R;
import com.lujianfei.productprice.base.BaseActivity;
import com.lujianfei.productprice.base.CommonAdapter;
import com.lujianfei.productprice.base.ViewHolder;
import com.lujianfei.productprice.ui.order.OrderActivity;

import java.util.ArrayList;
import java.util.List;

public class CartActivity extends BaseActivity implements CartContract.View {

    CartContract.Presenter presenter;
    BaseAdapter baseAdapter;
    List<CartEntity> cartEntities = new ArrayList<>();
    ListView listview;
    TextView txt_total_price;
    Button bt_submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        setTitleMiddleText("购物车");
        listview = findViewById(R.id.listview);
        txt_total_price = findViewById(R.id.txt_total_price);
        bt_submit = findViewById(R.id.bt_submit);
        presenter = new CartPresenter(this);
        presenter.loadData();
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean submit = presenter.submit();
                if (submit){
                    startActivity(new Intent(CartActivity.this,OrderActivity.class));
                    finish();
                }
            }
        });
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                CartEntity cartEntity = (CartEntity) parent.getItemAtPosition(position);
                presenter.showLongClickDialog(cartEntity);
                return true;
            }
        });
    }

    @Override
    protected int getTitleBarResId() {
        return R.id.custom_titlebar;
    }

    @Override
    public long getShopId() {
        return getIntent().getLongExtra("shopId",-1);
    }

    @Override
    public void setData(List<CartEntity> cartEntities) {
        if (null == baseAdapter){
            baseAdapter = new CommonAdapter<CartEntity>(this,this.cartEntities,R.layout.adapter_cart_item) {
                @Override
                public void convert(ViewHolder viewHolder, final CartEntity data) {
                    viewHolder.setText(R.id.txtShopName,data.getShopName());
                    viewHolder.setText(R.id.txtProductName,data.getProductName());
                    viewHolder.setText(R.id.txtProductPrice,String.format("¥ %.2f",data.getProductPrice()));
                    viewHolder.setText(R.id.txt_quantity,""+data.getQuantity());
                    ImageView iv_delete = viewHolder.getView(R.id.iv_delete);
                    ImageView iv_add = viewHolder.getView(R.id.iv_add);

                    iv_delete.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            presenter.minusQuantity(data);
                        }
                    });
                    iv_add.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            presenter.addQuantity(data);
                        }
                    });
                }
            };
            listview.setAdapter(baseAdapter);
        }
        this.cartEntities.clear();
        this.cartEntities.addAll(cartEntities);
        baseAdapter.notifyDataSetChanged();
    }

    @Override
    public void setTotalPrice(double totalPrice) {
        txt_total_price.setText(String.format("总价：¥ %.2f",totalPrice));
    }

    @Override
    public String getShopName() {
        return getIntent().getStringExtra("shopName");
    }

    @Override
    public Context getContext() {
        return this;
    }
}
